package com.liaowei.system.controller;

import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.Tag;
import com.liaowei.model.vo.TagVO;
import com.liaowei.system.model.param.TagParam;
import com.liaowei.system.service.TagService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("tags")
@RequiredArgsConstructor
@Validated
public class TagController {

    private final TagService tagService;


    // 根据id查询标签列表
    @PostMapping("list")
    public BaseResponse<List<Tag>> tagList(@RequestBody List<Long> tagIdList) {
        return ResultUtils.success(tagService.searchListByIds(tagIdList));
    }


    // 根据标签id集合查询标签列表
    @PostMapping("count/ids")
    public BaseResponse<Integer> countByIds(@RequestBody
                                               @NotNull(message = "标签不能为空")
                                               List<Long> tagIdList){
        return ResultUtils.success(tagService.searchCountByIds(tagIdList));
    }


    // 查询树状标签
    @GetMapping("tree")
    public BaseResponse<List<TagVO>> tree() {
        return ResultUtils.success(tagService.treeList());
    }

    // 根据id删除标签
    @DeleteMapping("manage/{id}")
    public BaseResponse<String> deleteTag(@PathVariable
                                          @Min(value = 1, message = "参数无效")
                                          long id) {
        return ResultUtils.success(tagService.deleteTagById(id));
    }

    // 保存标签
    @PostMapping("manage")
    public BaseResponse<String> saveTag(@RequestBody @Validated TagParam tagParam) {
        return ResultUtils.success(tagService.saveTag(tagParam));
    }

}
